{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0341a89e3dc0"
   },
   "source": [
    "##### Copyright 2020 The Cirq Developers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "906e07f6e562"
   },
   "outputs": [],
   "source": [
    "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "f6f750b81e1d"
   },
   "source": [
    "# AQT Cirq Tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dcea2d63ce2a"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/hardware/aqt/getting_started\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/main/docs/hardware/aqt/getting_started.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/main/docs/hardware/aqt/getting_started.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/hardware/aqt/getting_started.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bd9529db1c0b"
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    import cirq\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install --quiet cirq\n",
    "    print(\"installed cirq.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "b42eeeef4398"
   },
   "source": [
    "[AQT](https://www.aqt.eu) supports Cirq as a third party software development kit and offers access to quantum computing devices and simulators in the backend. Visit [www.aqt.eu](https://www.aqt.eu/qc-systems/) to find available resources and information on how to get access to them.\n",
    "\n",
    "After the Cirq installation has finished successfully, you are ready to use the offline simulator or different backends through the use of an access token and the corresponding parameters, as in the following getting started tutorial.\n",
    "\n",
    "## Use your AQT credentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "4e87b1af4806"
   },
   "outputs": [],
   "source": [
    "import cirq\n",
    "from cirq.aqt.aqt_device import get_aqt_device\n",
    "from cirq.aqt.aqt_sampler import AQTSampler\n",
    "\n",
    "access_token = 'MY_TOKEN'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "63a64281ca4e"
   },
   "source": [
    "Where `MY_TOKEN` is your access token for the AQT Arnica API. Then you can retrieve the information which workspaces and quantum resources are available for you:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "29a2d83f816f"
   },
   "outputs": [],
   "source": [
    "AQTSampler.fetch_resources(access_token)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "322966c20c47"
   },
   "source": [
    "Then you can specify the workspace and resource you want to send your quantum circuits to."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "4fbc40903531"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0───1\n"
     ]
    }
   ],
   "source": [
    "workspace = 'WORKSPACE_NAME'\n",
    "resource = 'RESOURCE_NAME'\n",
    "aqt_sampler = AQTSampler(workspace=workspace, resource=resource, access_token=access_token)\n",
    "\n",
    "device, qubits = get_aqt_device(2)\n",
    "print(device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "576763bf5e42"
   },
   "source": [
    "## Sample a quantum circuit\n",
    "\n",
    "You can then use that device in Cirq. For example, preparing the Bell state\n",
    "\n",
    "$$ |\\psi\\rangle=\\frac{1}{\\sqrt{2}}(|00\\rangle-\\mathrm{i}|11\\rangle) $$\n",
    "\n",
    "by writing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "37a9927421ac"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: ───XX───────\n",
      "      │\n",
      "1: ───XX^0.5─── [cirq.LineQubit(0), cirq.LineQubit(1)]\n"
     ]
    }
   ],
   "source": [
    "circuit = cirq.Circuit(cirq.XX(qubits[0], qubits[1]) ** 0.5)\n",
    "device.validate_circuit(circuit)\n",
    "print(circuit, qubits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8cad055ecdb1"
   },
   "source": [
    "This circuit can then be sampled on the real-hardware backend as well as on a simulator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "45cd8ccb7919"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m=1010010001010001111001001111000110010000111000011011110110101010010101001101111100011001000000100010, 1010010001010001111001001111000110010000111000011011110110101010010101001101111100011001000000100010\n"
     ]
    }
   ],
   "source": [
    "aqt_sweep = aqt_sampler.run(circuit, repetitions=100)\n",
    "print(aqt_sweep)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a6c7fd3190fe"
   },
   "source": [
    "**Note:** At the moment, the ```run()``` method of the AQTSampler implicitly performs measurements on all qubits at the end of the circuit, so explicit measurement operations aren't _required_. In fact, using explicit measurements apart from _exactly one at the end_ will cause the AQTSampler to fail. More fine-grained measurement operations will be added to the AQT Arnica API in the future."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "c469db551c46"
   },
   "source": [
    "## Offline simulation of AQT devices\n",
    "\n",
    "The AQT simulators are capable of running ideal simulations (without a noise model) and real simulations (with a noise model) of a quantum circuit. Using a simulator with noise model allows you to estimate the performance of running a circuit on the real hardware. Switching between the two simulation types is done by setting the simulate_ideal flag, as in the example below.\n",
    "\n",
    "For running a simulation without noise model use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "fe6a1aba2db9"
   },
   "outputs": [],
   "source": [
    "from cirq.aqt.aqt_sampler import AQTSamplerLocalSimulator\n",
    "\n",
    "aqt_sampler = AQTSamplerLocalSimulator(simulate_ideal=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a0d027509bcb"
   },
   "source": [
    "whereas for a simulation with noise model use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "d9c75a68a6e2"
   },
   "outputs": [],
   "source": [
    "from cirq.aqt.aqt_sampler import AQTSamplerLocalSimulator\n",
    "\n",
    "aqt_sampler = AQTSamplerLocalSimulator(simulate_ideal=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5108669495f5"
   },
   "source": [
    "Then you can use the Sampler Simulator as you would the regular one, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "4ac3e6439e13"
   },
   "outputs": [],
   "source": [
    "aqt_sweep = aqt_sampler.run(circuit, repetitions=100)\n",
    "print(aqt_sweep)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "getting_started.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
